"""
定义模型 基本和振动的一致 区别只是输入的维度变了
"""
import torch
from torch import nn
from torch.nn import functional as F


class BaseBlock(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride, padding=0):
        super(BaseBlock, self).__init__()
        self.conv = nn.Conv1d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding)
        self.bn = nn.BatchNorm1d(out_channels)
        self.act = nn.ELU()

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.act(x)
        return x


class SAFDNN_AE(nn.Module):
    def __init__(self, input_size, output_size):
        super(SAFDNN_AE, self).__init__()
        self.conv1 = BaseBlock(input_size, 32, 64, 8, 31)
        self.pool1 = nn.MaxPool1d(2, 2)

        self.conv2 = BaseBlock(32, 64, 3, 1, 1)
        self.pool2 = nn.MaxPool1d(2, 2)

        self.lstm1 = nn.LSTM(64, 32, 1, bidirectional=True, batch_first=True)

        self.fc1 = nn.Linear(64, 64)
        self.fc2 = nn.Linear(128, 100)
        self.fc3 = nn.Linear(100, 32)
        self.fc4 = nn.Linear(32, output_size)

    def forward(self, x):
        x = self.conv1(x)
        x = self.pool1(x)

        x = self.conv2(x)
        x = self.pool2(x)
        x = x.permute(0, 2, 1)
        x, temp = self.lstm1(x)

        last_h = x[:, -1, :]  # (3, 64)
        temp = self.fc1(x)  # (3, 25, 64)
        temp = torch.bmm(temp, last_h.unsqueeze(2)).squeeze(2)
        temp = F.sigmoid(temp)
        temp = torch.bmm(temp.unsqueeze(1), x).squeeze(1)
        temp = torch.concat((last_h, temp), dim=1)
        x = self.fc2(temp)

        x = self.fc3(x)
        x = self.fc4(x)
        return x


if __name__ == "__main__":
    model = SAFDNN_AE(1, 2)
    # print(model.state_dict().keys())
    x = torch.randn(3, 1, 1000000)
    y = model(x)
    print(y.shape)